﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Threading;
using System.Xml.Linq;

namespace ontheroad
{
    public class ThaiTraffyApi
    {
        public ThaiTraffyApi()
        {
        }

        public void getCongestion(string roadName,List<trafficCongestion> linkList)
        {
            WebClient wcf = new WebClient();
            linkList = new List<trafficCongestion>();
            Uri uri = new Uri("http://athena.traffy.in.th/apis/apitraffy.php?format=XML&api=getLinkALLInfo&key="
                + App.traf.sessionkey + "&appid=" + App.traf.appid);
            wcf.DownloadStringAsync(uri);
            wcf.DownloadStringCompleted += (s, e) =>
                {
                    XElement elm = XElement.Parse(e.Result);
                    while (elm.FirstNode != null)
                    {
                        string roadname = ((elm.FirstNode as XElement).FirstNode as XElement).FirstAttribute.Value;
                        trafficCongestion cgc = new trafficCongestion();
                        cgc.road.linkID = (elm.FirstNode as XElement).FirstAttribute.Value;
                        cgc.road.startNode = (((elm.FirstNode as XElement).FirstNode as XElement).NextNode as XElement).FirstAttribute.Value;
                        cgc.road.endNode = ((((elm.FirstNode as XElement).FirstNode as XElement).NextNode as XElement).NextNode as XElement).FirstAttribute.Value;
                        cgc.road.roadName = roadname;
                        if (roadname.Contains(roadName))
                            linkList.Add(cgc);
                        elm.FirstNode.Remove();
                    }
                    WebClient wcf2 = new WebClient();
                    WebClient wcf3 = new WebClient();
                    string uri2name = "http://api.traffy.in.th/apis/apitraffy.php?format=XML&api=getCL&key="
                        + App.traf.sessionkey + "&appid=" + App.traf.appid + "&linkid=";
                    string uri3name = "http://api.traffy.in.th/apis/apitraffy.php?api=getPCL&appid="
                        + App.traf.appid + "&key=" + App.traf.sessionkey + "&linkid=";
                    for (int i = 0; i < linkList.Count; i++)
                    {
                        uri2name += linkList[i].road.linkID + ((i + 1 == linkList.Count ? "" : ","));
                        uri3name += linkList[i].road.linkID + ((i + 1 == linkList.Count ? "" : ","));
                    }
                    uri3name += "&format=XML";
                    Uri uri2 = new Uri(uri2name);
                    Uri uri3 = new Uri(uri3name);
                    wcf2.DownloadStringAsync(uri2);
                    wcf2.DownloadStringCompleted += (s1, e1) =>
                        {
                            XElement elm2 = XElement.Parse(e1.Result);
                            while (elm2.FirstNode != null)
                            {
                                string linkid = ((elm2.FirstNode as XElement).FirstNode as XElement).Value;
                                int i;
                                for (i = 0; linkList[i].road.linkID != linkid; i++) ;
                                trafficCongestion cgc = linkList[i];
                                cgc.timeCongestion = Convert.ToDateTime((((elm2.FirstNode as XElement).FirstNode as XElement).NextNode as XElement).Value);
                                string conType = ((((elm2.FirstNode as XElement).FirstNode as XElement).NextNode as XElement).NextNode as XElement).Value;
                                cgc.congestionType = (conType == "H") ? "ติดขัด" : ((conType == "M" ? "หนาแน่น" : "คล่องตัว"));
                                elm2.FirstNode.Remove();
                            }
                            //MessageBox.Show("");
                            wcf3.DownloadStringAsync(uri3);
                            wcf3.DownloadStringCompleted += (s2, e2) =>
                            {
                                XElement elm3 = XElement.Parse(e2.Result);
                                while (elm3.FirstNode != null)
                                {
                                    string linkid = ((elm2.FirstNode as XElement).FirstNode as XElement).Value;
                                    int i;
                                    for (i = 0; linkList[i].road.linkID != linkid; i++) ;
                                    trafficCongestion cgc = linkList[i];
                                    string conType = (((((elm3.FirstNode as XElement).FirstNode as XElement).NextNode as XElement).NextNode as XElement).NextNode as XElement).Value;
                                    switch (Convert.ToInt32(conType))
                                    {
                                        case 1: conType = "เคลื่อนตัวได้ดี"; break;
                                        case 2: conType = "เคลื่อนตัวได้เรื่อยๆ"; break;
                                        case 3: conType = "เคลื่อนตัวได้ตามจังหวะสัญญาณไฟ"; break;
                                        case 4: conType = "เคลื่อนตัวได้ตามปกติ"; break;
                                        case 5: conType = "เคลื่อนตัวช้า"; break;
                                        case 6: conType = "หนาแน่น"; break;
                                        case 7: conType = "ติดขัด"; break;
                                        case 0: conType = "ไม่สามารถระบุได้"; break;
                                    }
                                    cgc.preCongestionType = conType;
                                    elm2.FirstNode.Remove();
                                }
                                for (int i = 0; i < linkList.Count; i++)
                                    if (linkList[i].preCongestionType == "")
                                        linkList[i].preCongestionType = "ไม่สามารถระบุได้";
                            };
                        };
                    
                };
        }

    }
}
